package com.beadhouse.outregistration.web.controller;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.OutEntity;
import com.beadhouse.outregistration.ao.OutAo;
import com.beadhouse.outregistration.service.OutGoingService;
/**
 * @author admin 周彬
 */
@Controller
@RequestMapping("/outgoing")
public class OutGoingCountroller {
	@Autowired
	OutGoingService outGoingService;
	
	@GetMapping("/add")
	@ResponseBody
	public JSONResponse addOutgoing(OutAo ao) throws Exception{
		try {
			outGoingService.addRegistration(ao);
			return JSONResponse.ok();
		} catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		}
		
	}
	@GetMapping("/delete")
	@ResponseBody
	public JSONResponse deleteOutGoing(String id) throws Exception{
		try {
			outGoingService.deleteOG(id);
			return JSONResponse.ok();
		} catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		}
	
		
	}
	@GetMapping("/update")
	@ResponseBody
	public JSONResponse updateOutGoing(OutAo ao,String currentId) throws Exception{
		try {
			outGoingService.updateOG(ao, currentId);
			return JSONResponse.ok();
		} catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		}
	}
	@GetMapping("/updateTime")
	@ResponseBody
	public JSONResponse updateTimeOutGoing(String actualReturnTime,String customerId) throws Exception{
		try {
			outGoingService.updateTimeOG(actualReturnTime,customerId);
			return JSONResponse.ok();
		} catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		}
		
	}
	@GetMapping("/approval")
	@ResponseBody
	public JSONResponse approvalOutGoing(String id,String approval) throws Exception{
		try {
			outGoingService.approvalOG(id, approval);
			return JSONResponse.ok();
		} catch (Exception e) {
			return JSONResponse.fail(e);
		}
		
	}
	@GetMapping("/list")
	public ModelAndView listOutGoing(String key,String pageNo,String pageSize) throws Exception{
		try {
			ModelAndView mav=new ModelAndView("outgoingregistration/outgoingregistration");
			QueryResult<OutEntity> qr=outGoingService.selectlistOG(key,pageNo,pageSize);
			List<OutEntity> list=qr.getRows();
			Collections.reverse(list);
			mav.addObject("og",qr);
			return mav;
		} catch (BeadHouseException e) {
			throw new BeadHouseException("列表获取失败！");
		}
	}
	
	@GetMapping("/check")
	@ResponseBody
	public JSONResponse selectidOutGoing(String id) throws Exception{
		try {
			OutEntity oe=outGoingService.selectByIdOG(id);
			return JSONResponse.ok(oe);
		}catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		}
	}
	
	
}
